package org.masterinftel.scrumpmt.adapters;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.List;

import org.masterinftel.scrumpmt.Constants;
import org.masterinftel.scrumpmt.R;
import org.masterinftel.scrumpmt.adapters.ProductsListAdapter.ViewHolder;
import org.masterinftel.scrumpmt.json.model.ProductoJSON;
import org.masterinftel.scrumpmt.json.model.SprintJSON;

import android.app.Activity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class SprintsListAdapter extends ArrayAdapter<SprintJSON> {

	    private final Activity context;
	    private final List<SprintJSON> sprintsList;

	    public SprintsListAdapter(Activity context, List<SprintJSON> sprintsList) {
	        super(context, R.layout.sprintslistelement, sprintsList);
	        this.context = context;
	        this.sprintsList = sprintsList;
	    }

	    static class ViewHolder {
	        protected TextView des;
	        protected TextView startdate;
	        protected TextView enddate;

	    }

	    public View getView(int position, View convertView, ViewGroup parent) {
	        View item = null;
	        if (convertView == null) {
	            LayoutInflater inflater = context.getLayoutInflater();
	            item = inflater.inflate(R.layout.sprintslistelement, null);
	            final ViewHolder viewHolder = new ViewHolder();
	            viewHolder.des = (TextView) item.findViewById(R.id.sprint_des);
	            viewHolder.startdate = (TextView) item.findViewById(R.id.sprint_startdate);
	            viewHolder.enddate = (TextView) item.findViewById(R.id.sprint_enddate);
	            item.setTag(viewHolder);
	        } else {
	            item = convertView;

	        }
	        ViewHolder holder = (ViewHolder) item.getTag();
	        Log.i(Constants.LOGTAG, sprintsList.get(position).getDes());
	        holder.des.setText(sprintsList.get(position).getDes());
	        DateFormat format = new SimpleDateFormat("dd/MM/yyyy");
	        String fini = format.format(sprintsList.get(position).getFecini());
	        String ffin = format.format(sprintsList.get(position).getFecfin());
	        holder.startdate.setText("Inicio: "+fini);
	        holder.enddate.setText("Fin: "+ffin);

	        return (item);
	    }
}
